package neo.tetris.rpc.service;

import java.util.UUID;

import neo.sourceEngine.network.rpc.service.annotation.RpcService;
import neo.sourceEngine.network.rpc.service.annotation.RpcServiceParameter;
import neo.tetris.bean.model.World;
import neo.tetris.bean.persist.UserInstance;
import neo.tetris.rpc.message.MessageImpl;
import neo.tetris.rpc.session.SessionModel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @author feng.lee
 * @create on 2013-8-22
 */
@Service
public class UserService {
	static Logger logger = LoggerFactory.getLogger(UserService.class);

	@RpcService(desc = "玩家进入接口，新玩家则注册，老玩家则登陆，返回用户信息")
	public UserInstance login(SessionModel session,
			@RpcServiceParameter(name = "userName", desc = "玩家名字") String userName,
			@RpcServiceParameter(name = "headIcon", desc = "玩家名字") String headIcon) {
		UserInstance user = newUser(userName);
		user.headIcon = headIcon;
		session.setLogined(user);
		World.instance.loginedSessions.put(user.uuid, session);
		session.write(MessageImpl.systemMessage.setSystemTime(System.currentTimeMillis()));
		session.write(MessageImpl.userMessage.setLoginedUser(user.uuid, user.name, user.headIcon));
		return user;
	}

	public UserInstance newUser(String name) {
		UserInstance user = new UserInstance();
		user.uuid = UUID.randomUUID().toString();
		user.name = name;
		user.gold = 200;
		user.silver = 100;
		return user;
	}
}
